//
//  YMPasswordLoginView.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMPasswordLoginView.h"
#import "YMLoginInputBoxView.h"
#import  "YMLoginService.h"


@interface YMPasswordLoginView ()<YMLoginInputBoxViewDelegate>

/** 手机号 */
@property (nonatomic, strong) YMLoginInputBoxView *vMobile;
/** 密码 */
@property (nonatomic, strong) YMLoginInputBoxView *vPwd;

@property (nonatomic, copy) NSString *strMobile;
@property (nonatomic, copy) NSString *strPwd;

@end

@implementation YMPasswordLoginView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self addSubview:self.vMobile];
    [self addSubview:self.vPwd];
    
    [self.vMobile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
        make.top.mas_equalTo(0);
    }];
    [self.vPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(self.vMobile);
        make.top.equalTo(self.vMobile.mas_bottom).offset([SFitUtil fit:20]);
    }];

}

#pragma mark -- event action
- (void)loginClickAction{
    if (self.strMobile == nil || _strMobile.length < 1) {
        [SHUD showInfo:KLanguage(@"input_phone_txt")];
        return;
    }
    if (self.strMobile.length < 11) {
        [SHUD showInfo:KLanguage(@"input_right_phone_txt")];
        return;
    }
    if (self.strPwd == nil || self.strPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"input_pwd_txt")];
        return;
    }
//    if (self.strPwd.length < 8 || self.strPwd.length > 16) {
//        [SHUD showInfo:KLanguage(@"re_input_right_pwd_txt")];
//        return;
//    }
    if (self.isAgree == NO) {
        [SHUD showInfo:KLanguage(@"read_agree_ag_txt")];
        return;
    }
    WEAK_SELF;
    [YMLoginService requestUserAccountLoginWithPhone:self.strMobile password:self.strPwd success:^{
        [weakSelf.viewController.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
    
}
#pragma mark -- YSLoginInputBoxViewDelegate
- (void)textFiledChanged:(UITextField *)sender{
    if (sender.tag == 200) {
        if (sender.text.length > 11) {
            sender.text = [sender.text substringToIndex:11];
        }
        self.strMobile = sender.text;
    }else if (sender.tag == 201){
        if (sender.text.length > 18) {
            sender.text = [sender.text substringToIndex:18];
        }
        self.strPwd = sender.text;
    }
    [self updateButttonStatus];
}

- (void)updateButttonStatus{
    BOOL isCan = NO;
    if (self.strMobile != nil && _strMobile.length == 11 &&
        self.strPwd != nil && self.strPwd.length > 7 && self.strPwd.length < 17) {
        isCan = YES;
    }
    if (self.changeButtonStautsBlock){
        self.changeButtonStautsBlock(isCan);
    }
}


#pragma mark -- setter and getter
- (YMLoginInputBoxView *)vMobile{
    if (_vMobile == nil) {
        _vMobile = [[YMLoginInputBoxView alloc] init];
        _vMobile.strTitle = KLanguage(@"phone_txt");
        _vMobile.strPlace = KLanguage(@"input_txt");
        _vMobile.delegate = self;
        _vMobile.tfTag = 200;
        _vMobile.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _vMobile;
}
- (YMLoginInputBoxView *)vPwd{
    if (_vPwd == nil) {
        _vPwd = [[YMLoginInputBoxView alloc] init];
        _vPwd.strTitle = KLanguage(@"password_txt");
        _vPwd.strPlace = KLanguage(@"input_txt");
        _vPwd.delegate = self;
        _vPwd.tfTag = 201;
        _vPwd.isPwd = YES;
        _vPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vPwd;
}

@end
